package com.example.yan_music.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.yan_music.R;
import com.example.yan_music.db.table.HistoryRecord;
import com.example.yan_music.presenter.ISearchPresenterImp;
import com.example.yan_music.utils.DipAndPxUtils;
import com.example.yan_music.widge.XFlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *     流式布局的适配器
 *     需要在这里给子项设置margin，xml设置不生效
 * </p>
 */
public class XFlowAdapter extends XFlowLayout.Adapter<XFlowAdapter.FlowViewHolder> {

    private static final String TAG = "CflAdapter";

    private List<HistoryRecord> mHistoryList = new ArrayList<>();
    private ISearchPresenterImp iSearchPresenterImp;
    private Context mContext;
    private OnSearchFromHistory mListener;

    public XFlowAdapter(List<HistoryRecord> mHistoryList, ISearchPresenterImp iSearchPresenterImp, Context mContext) {
        this.mHistoryList = mHistoryList;
        this.iSearchPresenterImp = iSearchPresenterImp;
        this.mContext = mContext;
    }

    @Override
    public FlowViewHolder onCreateViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.clf_history_item, parent,false);
        // 设置margin
        setItemMargin(view);
        return new FlowViewHolder(view);
    }

    @Override
    public void onBindViewHolder(FlowViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return mHistoryList.size();
    }

    /**
     * ViewHolder
     */
    class FlowViewHolder extends XFlowLayout.ViewHolder{
        private TextView tv_record_name;
        // 字段值，这个值是从历史记录表中取出的数据
        private String historyRecord;
        private int position;

        public FlowViewHolder(View itemView) {
            super(itemView);
            tv_record_name = itemView.findViewById(R.id.tv_clf_record_name);
            tv_record_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // iSearchPresenterImp.search(historyRecord, 1);
                    mListener.onHistorySearch(historyRecord);
                }
            });
        }

        public void setData(int position) {
            this.position = position;
            historyRecord = mHistoryList.get(position).getRecordName();
            tv_record_name.setText(historyRecord);
            Log.d(TAG, "setData: " + "当前子项的位置position = [ " + position + " ]，给子项控件TV设的字段值为：[ " + historyRecord + " ]");
        }
    }

    /**
     * 给子项设置margin
     *
     * @param view
     */
    public void setItemMargin(View view){
        ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(view.getLayoutParams());
        // 设置子项的margin
        int margin = DipAndPxUtils.dip2px(mContext, 5);
        marginLayoutParams.setMargins(margin, margin ,margin, margin);
        view.setLayoutParams(marginLayoutParams);
    }

    /**
     * 子项点击事件接口
     */
    public interface OnSearchFromHistory{
        void onHistorySearch(String historyRecord);
    }

    /**
     * 设置子项点击事件
     *
     * @param listener
     */
    public void setOnSearchFromHistory(OnSearchFromHistory listener){
        this.mListener = listener;
    }
}
